package xyz.yeidi.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import xyz.yeidi.pojo.Food;
import xyz.yeidi.service.api.FoodService;
import xyz.yeidi.utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author JIP
 * @Date 2021-08-03 21:02
 * @Version 1.0
 * @Describe :  通过商家的id得到所有的其所有的食物
 */
@WebServlet("/getFoods")
public class GetFoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        FoodService foodService = ServiceFactory.getFoodService();
        String id = request.getParameter("businessId");
        List<Food> foods = foodService.getFoodsByBusiness(Integer.valueOf(id));
        ObjectMapper om = new ObjectMapper();
        PrintWriter out = response.getWriter();
        if(foods.isEmpty()){
            out.print("no_foods");
        }else{
            out.print(om.writeValueAsString(foods));
        }
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
